package com.pig4cloud.pig.djjk.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.djjk.entity.TrackAnalysisEntity;
import com.pig4cloud.pig.djjk.mapper.TrackAnalysisMapper;
import com.pig4cloud.pig.djjk.service.TrackAnalysisService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 抓拍统计表
 *
 * @author pig
 * @date 2025-06-16 14:26:46
 */
@Service
public class TrackAnalysisServiceImpl extends ServiceImpl<TrackAnalysisMapper, TrackAnalysisEntity> implements TrackAnalysisService {


	@Override
	@Transactional
	@SneakyThrows
	public void updateTrackAnalysis(TrackAnalysisEntity trackAnalysis){
		Optional<TrackAnalysisEntity> trackAnalysisServiceOneOpt = this.getOneOpt(new LambdaQueryWrapper<TrackAnalysisEntity>().eq(TrackAnalysisEntity::getDeviceCode, trackAnalysis.getDeviceCode())
				.eq(TrackAnalysisEntity::getTheDate, DateUtil.today()), false);
		if(trackAnalysisServiceOneOpt.isPresent()){
			TrackAnalysisEntity trackAnalysisEntity = trackAnalysisServiceOneOpt.get();
			trackAnalysisEntity.setTrackCount(trackAnalysisEntity.getTrackCount()+trackAnalysis.getTrackCount());
			trackAnalysisEntity.setMatchCaount(trackAnalysisEntity.getMatchCaount()+trackAnalysis.getMatchCaount());
			trackAnalysisEntity.setNoMatchCaount(trackAnalysisEntity.getNoMatchCaount()+trackAnalysis.getNoMatchCaount());
			trackAnalysisEntity.setMatchPersonCount(trackAnalysisEntity.getMatchPersonCount()+trackAnalysis.getMatchPersonCount());
			this.updateById(trackAnalysisEntity);
		}else{
			trackAnalysis.setTrackCount(trackAnalysis.getTrackCount());
			trackAnalysis.setNoMatchCaount(trackAnalysis.getNoMatchCaount());
			trackAnalysis.setMatchCaount(trackAnalysis.getMatchCaount());
			trackAnalysis.setTheDate(DateUtil.today());
			trackAnalysis.setMatchPersonCount(trackAnalysis.getMatchPersonCount());
			this.save(trackAnalysis);
		}
	}
}
